weiXinController.controller('slrOrderPageCtl',['$scope','$state','$stateParams','$timeout','$interval','$compile','$rootScope',function($scope,$state,$stateParams,$timeout,$interval,$compile,$rootScope) {
	console.log({name:$state.current.name,templateUrl:$state.current.templateUrl});
	
	/**
	 * 查询初始化
	 */
	$scope.orderSearchObj = {type_account:'',type:'',type_from:'',approval_status:'',status:'',approval_type:''};

	var param = {
		id : 'tableDemo',
		checkbox : false,
		column : [
			{
				type:'rowNum',
				title:'编号',
				titleTdStyle: 'width: 100px;color: #17a2b8;'
			},{
				type:'key',
				mapping:'id',
				show:false
			},{
				mapping: 'name',
            	title:'仓库',
			},{
                mapping:'type',
                title:'类型',
                trans:function(data){
                    if(data.type === 'OUT'){
                        return '出库';
                    }
                    if(data.type === 'BORROW'){
                        return '借出';
                    }
                }
            },{
				mapping: 'type_from',
            	title:'来源',
            	trans:function(data){
            		if(data.type_from === 'IN'){
                		return '自建';
                	}
                	if(data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL'){
                		return '同步';
                	}
            	}
			},{
                mapping:'approval_type',
                title:'审批方式',
                trans:function(data){
                    if(data.approval_type === 'COMMON'){
                        return '正常';
                    }
                    if(data.approval_type === 'URGENT'){
                        return '紧急';
                    }
                    if(!data.approval_type){
                        return '无';
                    }
                }
            },{
				mapping: 'approval_status',
            	title:'审批状态',
            	trans:function(data){
            		 if(data.approval_status === 'INIT'){
                     	return '未提交';
                     }
                     if(data.approval_status === 'CHECKING'){
                    	 return '审批中';
                     }
                     if(data.approval_status === 'PASS'){
                    	 return '通过';
                     }
                     if(data.approval_status === 'REJECT'){
                    	 return '驳回';
                     }
                    if(!data.approval_status){
                        return '无';
                    }
            	}
			},{
				mapping: 'type_account',
            	title:'备件类型',
            	trans:function(data){
            		  if(data.type_account === 'IN'){
                      	return '账内';
                      }
                      if(data.type_account === 'OUT'){
                      	return '账外';
                      }
            	}
			},{
				mapping: 'status',
            	title:'状态',
            	trans:function(data){
	        		 if(data.status === 'INIT'){
	                 	return '未完成';
	                 }
	                 if(data.status === 'DOING'){
	                	 return '处理中';
	                 }
	                 if(data.status === 'DONE'){
	                	 return '已完成';
	                 }
	                 if(data.status === 'BINDED'){
	                	 return '已绑定';
	                 }
	                 if(data.status === 'BALANCED'){
	                	 return '已冲销';
	                 }
	                 if(data.status === 'CANCEL'){
	                	 return '已取消';
	                 }
                     if(data.status === 'UNBACK'){
                         return '待归还';
                     }
            	}
			},{
                mapping:'password',
                title:'密码',
                trans:function(data){
                    if(data.status === 'INIT' && data.approval_status === 'PASS'){
                        return data.password;
                    }
                    if(data.status === 'INIT' && (data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL')){
                        return data.password;
                    }
                    return '无';
                }
            },{
                mapping:'gmt_modified',
                title:'操作时间',
                trans:function(data){
            		return data.gmt_modified ;
                }
			}, {
                type: 'function',
                title: '操作',
                titleDivStyle: 'text-align: center',
                dataDivStyle: 'text-align: center',
                func: [{
                    title: '提交',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-angle-double-up" style="width: 15px;"></i>',
                    text: '提交',
                    show: function(data){
                        return (data.status === 'INIT' && data.approval_status === 'INIT');
                    },
                    func: function (data) {
                        $scope.order(data.id, data.warehouse_info_id);
                    }
                },{
                    title: '修改',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-pencil-alt"></i>',
                    text: '修改',
                    show: function(data){
                        return (data.status === 'INIT' && data.approval_status === 'INIT');
                    },
                    func: function (data) {
                        $scope.update(data.id);
                    }
                },{
                    title: '查看',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-eye"></i>',
                    text: '查看',
                    func: function (data) {
                        $scope.orderView(data.id);
                    }
                },{
                    title: '取消',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-ban"></i>',
                    text: '取消',
                    show: function(data){
                        if(data.status === 'DOING' || data.status === 'DONE' || data.status === 'BINDED'
                        	|| data.status === 'BALANCED' || data.status === 'CANCEL'
                            || data.status === 'UNBACK' || data.status === 'CANCEL'){
                            return false; // 处理中/已完成/已绑定/已冲销/已取消/待归还
                        }
                        if(data.approval_status === 'REJECT'){
                            return false; // '驳回';
                        }
                        if(data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL'){
                            return false; // '同步';
                        }
                        return true;
                    },
                    func: function (data) {
                        $scope.orderCancel(data.id);
                    }
                },{
                    title: '审批结果',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-info-circle"></i>',
                    text: '审批结果',
                    show: function(data){
                        // 只有自建+状态为初始化的才能取消
                        return (data.status === 'INIT' && (data.approval_status === 'PASS' || data.approval_status === 'REJECT'));
                    },
                    func: function (data) {
                        $scope.orderResult(data);
                    }
                },{
                    title: '审批查看',
                    css: 'btn-sm btn-outline-info',
                    text: '审批查看',
                    show: function(data){
                        if(data.approval_status === 'CHECKING'){
                            // return '审批中';
                            return true;
                        }
                        if(data.approval_status === 'PASS'){
                        	// return '通过';
                            return true;
                        }
                        if(data.approval_status === 'REJECT'){
                        	// return '驳回';
                            return true;
                        }
                    },
                    func: function (data) {
                        $scope.showApprovalStatus(data);
                    }
                }
                // ,{
                //     title: '审批结果',
                //     css: 'btn-sm btn-outline-info',
                //     text: '<i class="fas fa-info-circle"></i>',
                //     show: function(data){
                //         // 同步的申领单
                //         return (data.status === 'INIT' && (data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL'));
                //     },
                //     func: function (data) {
                //         $scope.orderResult(data, "TB");
                //     }
                // }
                ,{
                    title: '备件位置',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-map-marker-alt"></i>',
                    text: '备件位置',
                    show: function(data) {
                        return (data.status === 'INIT' && (data.approval_status === 'PASS' || data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL'));
                    },
                    func: function (data) {
                        $scope.materielLocation(data);
                    }
                 }
                ,{
                    title: '库存校验',
                    css: 'btn-sm btn-outline-info',
                    text111: '<i class="fas fa-allergies"></i>',
                    text: '库存校验',
                    show: function(data){
                        return (data.status === 'INIT' && (data.approval_status === 'PASS' || data.type_from === 'OUT_COMMON' || data.type_from === 'OUT_CANCEL'));
                    },
                    func: function (data) {
                        $scope.inventoryCheck(data);
                    }
                }]
            }
		],
        pageConfig:{
        	showGoNum:false
        },
		page:function(cmd, currentPage, pageSize, totalPageNum){
			if('first' === cmd){
				currentPage = 1;
			}else if('prev' === cmd){
				currentPage--;
			}else if('next' === cmd){
				currentPage++;
			}else if('last' === cmd){
				currentPage = totalPageNum;
			}else if('change' === cmd){
				currentPage = 1;
			}
			$scope.search(currentPage,pageSize);
		}
	};
	EdeyeAdmin.initDataTablePhone(param, {data:[]});
	
	/**
	 * 搜索
	 */
	$scope.search = function(currentPage,pageSize){
		var postData = $scope.orderSearchObj;
		postData.currentPage = (currentPage === undefined ? 1 : currentPage);
		postData.pageSize = (pageSize === undefined ? EdeyeAdmin.getPageSizePhone(param.id) : pageSize);
		EdeyeAdmin.post({
			url:'/orderInfo/slrPage',
			data:postData
		},{
			SUCCESS:function(ret){
				if(ret.code === '200'){
					EdeyeAdmin.setDataTableDataPhone(param, ret.data);
				}
			}
		});
	}
	$scope.search();
	
	function doHandleMonth(month){
		var m = month;
		if(month.toString().length == 1){
			m = "0" + month;
		}
		return m;
	}
	
	// 同步申领单
	$scope.sync = function(){
		var postData = {};
    	var date = EdeyeAdmin.defaultDateTime();
    	postData.endDate = date.split(' ')[0];
    	
    	var today = new Date();
	 　　var targetday_milliseconds=today.getTime() + 1000*60*60*24*(-7);
	 　　today.setTime(targetday_milliseconds);
	 　　var tYear = today.getFullYear();
	 　　var tMonth = today.getMonth();
	 　　var tDate = today.getDate();
	 　　tMonth = doHandleMonth(tMonth + 1);
	 　　tDate = doHandleMonth(tDate);
    	postData.startDate = tYear+"-"+tMonth+"-"+tDate;
    	
    	var layerId = layer.confirm('同步大概需要几分钟,确认同步?', {
            btn: ['确认', '取消']
        }, function () {
        	layer.close(layerId);
        	EdeyeAdmin.openMsg('申领单正在同步',true);
        	EdeyeAdmin.post({
        		url:'/orderInfo/contrast',
        		data:postData
        	},{
        		SUCCESS:function(ret){
	        		EdeyeAdmin.closeChangeMsg('请勿重复调用,稍后刷新页面',true,6);
        		}
        	});
        });
	}

    // 提交申领单
    $scope.order = function(id,warehouse_id){
        // TODO 二次确认 注释 4月8日
        // layer.confirm('是否提交申领单?', {
        //     btn: ['确认','取消'],
        //     closeBtn: false
        // }, function(){
            EdeyeAdmin.post({
                url: '/orderInfo/order',
                data: {
                	id:id,
                    warehouse_id:warehouse_id
                }
            }, {
                SUCCESS: function (ret) {
                	if(ret.code === '200'){
                		$scope.search();
                	}
                	EdeyeAdmin.closeMsg(ret.msg,false,3);
                }
            });
            layer.closeAll();
        // });
    }


    // 入库单取消
    $scope.orderCancel = function(id){
    	layer.confirm('是否取消申领单?', {
    		btn: ['确认','取消'],
    		closeBtn: false
        }, function(){
            EdeyeAdmin.post({
            	url: '/orderInfo/orderCancel',
            	data: {id:id}
            }, {
            	SUCCESS: function (ret) {
            		$scope.search();
            		EdeyeAdmin.closeMsg(ret.msg,true,1);
            	}
            });
            layer.closeAll();
        });
    }
    
    $scope.update = function (id) {
        $state.go('/slr/order/edit', {param:EdeyeAdmin.zip({'id':id})});
    }

    var scrollTop;

    // 申领单查看
    $scope.orderView = function(id){
        EdeyeAdmin.post({
            url: '/orderInfo/getOrderRecordById',
            data: {
                id:id,
                status:'INPUT'
                }
        }, {
            SUCCESS: function (ret) {
                console.log(ret);
                $scope.orderRecordViewList = ret.data;
                $scope.$apply();
                console.log(ret);
                $('#viewStoreModal').modal('show');

                scrollTop = document.scrollingElement.scrollTop;
                EdeyeAdmin.stopScroll(scrollTop);
            }
        });
    }

    $scope.hide = function () {
        $('#viewStoreModal').modal('hide');
        $('#viewMaterielLocationModal').modal('hide');
        EdeyeAdmin.openScroll(scrollTop);
    }

    //结果查看
    $scope.orderResult = function (data, type) {
        scrollTop = document.scrollingElement.scrollTop;
        EdeyeAdmin.stopScroll(scrollTop);

        var m = JSON.parse(data.approval_msg);
        var title = '';
        var msg = '';
        if(type == 'TB'){
            title = '通过';
            // msg = '门锁密码：<span style="color:red; font-weight:bold;">' + data.password + '</span></br>';
        }else {
            if(data.approval_status === 'PASS'){
                title = '通过';
                // msg = '门锁密码：<span style="color:red; font-weight:bold;">' + data.password + '</span></br>';
                for(var i = 0; i < m.length; i++){
                    if(m[i].msg){
                        msg+= '审批人：' + m[i].name + '</br>原因：' + m[i].msg + '</br>' + '操作时间：'+m[i].gmt_create + '</br>';
                    }else{
                        msg+= '审批人：' + m[i].name + '</br>' + '操作时间：'+m[i].gmt_create + '</br>';
                    }
                }
                //
            }else if(data.approval_status === 'REJECT'){
                title = '驳回';
                console.info(m);
                msg = '审批人：'+ m[m.length-1].name + '</br>原因：' + m[m.length-1].msg;
            }
        }

        layer.alert(msg, {
            title:title,
            closeBtn:0,
            skin: 'layui-layer-lan',
            btn: ['确定'], //按钮
            yes:function(){
                EdeyeAdmin.openScroll(scrollTop);
                layer.closeAll();
            }//样式类名
        });
    }

	// 查看审批状态
	$scope.showApprovalStatus = function(data){
		EdeyeAdmin.post({
			url:'/orderInfo/showApprovalStatus',
			data:{id:data.id}
		},{
			SUCCESS:function(ret){
				if(ret.code === '200'){
					console.log(ret);
					$scope.approvalLevels = ret.approvalLevel;
					$scope.approvalMsg = ret.approvalMsg;
					$scope.$apply();
					$('#viewApprovalModal').modal(true);
				}
			}
		});
	}

    //库存校验
    $scope.inventoryCheck = function (data) {
        EdeyeAdmin.post({
            url: '/orderInfo/inventoryCheck',
            data: {
                id:data.id,
            }
        }, {
            SUCCESS: function (ret) {
                EdeyeAdmin.closeMsg(ret.msg, true, 3);
            }
        });
    }

    //备件位置
    $scope.materielLocation = function (data) {
        console.info(data);
        EdeyeAdmin.post({
            url: '/orderInfo/getOrderMaterielLocation',
            data: {
                id:data.id,
                warehouse_info_id:data.warehouse_info_id
            }
        }, {
            SUCCESS: function (ret) {
                if(ret.code == 500){
                    EdeyeAdmin.closeMsg(ret.msg, true, 3);
                }else{
                    $scope.rfidLocationList = ret.rfid;
                    $scope.qrLocationList = ret.qr;
                    $scope.weightLocationList = ret.weight;
                    $scope.$apply();
                    scrollTop = document.scrollingElement.scrollTop;
                    EdeyeAdmin.stopScroll(scrollTop);
                    $('#viewMaterielLocationModal').modal('show');
                }

            }
        });
    }
}]);